#include<bits/stdc++.h>
#define x1 x_1
#define y1 y_1
#define x2 x_2
#define y2 y_2
#define mod 147744151
using namespace std;
char Begin;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
int n,m,P,len,cnt;
int val[2011][37][37];
int s[2011][37][37][37];
int a[1011][2011];
int L,lk,R,rk;
inline void ask(int l,int r)
{
	lk = (l + len - 1) / len;
	rk = (r + len - 1) / len;
	lk++,rk--;
	L = l % len;
	if(!L) L += len;
	R = r % len;
	if(!R) R += len;
}
typedef struct{
	int l,r,ls,rs;
}Tree;
Tree t[4011];
inline long long Gt(int p)
{
	long long res = 1;
	if(rk + 2 == lk)
	{
		return s[p][lk - 1][L][R];
	}
	if(lk <= rk) res = val[p][lk][rk];
	res = res * s[p][lk - 1][L][len] % P * s[p][rk + 1][1][R] % P;
	return res;
}
long long Ask(int p,int l,int r)
{
	if(l <= t[p].l && t[p].r <= r)
	{
		return Gt(p);
	}
	int mid = t[p].l + t[p].r >> 1;
	long long res = 1;
	if(l <= mid) res = Ask(t[p].ls,l,r);
	if(mid < r) res = res * Ask(t[p].rs,l,r) % P;
	return res;
}
inline long long Clac(int x1,int y1,int x2,int y2)
{
	ask(y1,y2);
	return Ask(0,x1,x2);
}
int idx;
void Build(int p,int l,int r)
{
	t[p].l = l,t[p].r = r;
	if(l == r)
	{
		for(int k = 1;k <= cnt;k++)
		{
			l = (k - 1) * len,r = k * len + 1;
			for(int i = 1;i <= len;i++)
			{
				s[p][k][i][i] = a[t[p].l][l + i];
				for(int j = i + 1;j <= len;j++)
				{
					s[p][k][i][j] = (long long)s[p][k][i][j - 1] * a[t[p].l][l + j] % P;
				}
			}
			val[p][k][k] = s[p][k][1][len];
		}
		for(int i = 1;i < cnt;i++)
		{
			for(int j = i + 1;j <= cnt;j++)
			{
				val[p][i][j] = (long long)val[p][i][j - 1] * val[p][j][j] % P;
			}
		}
		return;
	}
	int mid = l + r >> 1;
	Build(t[p].ls = ++idx,l,mid);
	Build(t[p].rs = ++idx,mid + 1,r);
	int ls = t[p].ls,rs = t[p].rs;
	for(int k = 1;k <= cnt;k++)
	{
		for(int i = 1;i <= len;i++)
		{
			for(int j = i;j <= len;j++)
			{
				s[p][k][i][j] = (long long)s[ls][k][i][j] * s[rs][k][i][j] % P;
			}
		}
	}
	for(int i = 1;i <= cnt;i++)
	{
		for(int j = i;j <= cnt;j++)
		{
			val[p][i][j] = (long long)val[ls][i][j] * val[rs][i][j] % P;
		}
	}
}
int Q,x1,y1,x2,y2;
long long res;
char End;
signed main()
{
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	n = read(),m = read(),P = read();
	len = sqrt(m);
	cnt = (m + len - 1) / len;
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= m;j++)
		{
			a[i][j] = read();
		}
	}
	Build(0,1,n);
	Q = read();
	for(int i = 1;i <= Q;i++)
	{
		x1 = read(),y1 = read(),x2 = read(),y2 = read();
		res = (res + (i ^ (Clac(x1,y1,x2,y2)))) % mod;
	}
	cout << res; 
	return 0;
}
